package com.xypower.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xypower.mapper.ClassesMapper;
import com.xypower.mapper.StudentMapper;
import com.xypower.pojo.Classes;
import com.xypower.pojo.Student;
import com.xypower.pojo.StudentExample;
import com.xypower.pojo.StudentVo;
import com.xypower.service.StudentService;
import com.xypower.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;
    private ClassesMapper classesMapper;
    public StudentServiceImpl(){
        //1. 得到sqlsession
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        //2. 得到studentMapper
        studentMapper = sqlSession.getMapper(StudentMapper.class);
        classesMapper = sqlSession.getMapper(ClassesMapper.class);
    }
    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectByExample(null);
        for (Student student : students){
            Integer cid = student.getCid();
            Classes classes = classesMapper.selectByPrimaryKey(cid);
            student.setCname(classes.getCname());
        }
        return students;
    }

    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void delete(String sid) {
        studentMapper.deleteByPrimaryKey(Integer.parseInt(sid));
    }

//    分页查询学生
    @Override
    public PageInfo<Student> findPage(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
//        进行分页查询
        StudentExample example = new StudentExample();
        List<Student> students = studentMapper.selectByExample(example);
//       绑定班级名称
        for (Student student:students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        PageInfo<Student> studentPageInfo =  new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public PageInfo<Student> search(int page, int pageSize, StudentVo vo) {
        PageHelper.startPage(page,pageSize);
        //创建实例
        StudentExample example = new StudentExample();
        //添加查询条件
        StudentExample.Criteria criteria = example.createCriteria();
        if (vo!=null){
            if (StrUtil.isNotBlank(vo.getSname())){
                criteria.andSnameLike("%"+vo.getSname()+"%");
            }
            if (StrUtil.isNotBlank(vo.getAddr())){
                criteria.andAddrLike("%"+vo.getAddr()+"%");
            }
            if (vo.getCid()!=0){
                criteria.andCidEqualTo(vo.getCid());
            }
        }
//        开始查询
        List<Student> students = studentMapper.selectByExample(example);
        //       绑定班级名称
        for (Student student:students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //返回
        return  new PageInfo<>(students);
    }
}
